﻿using Aker.DevTeam;
using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Drawing;

namespace Aker.Uretim
{
    public partial class ImalatReport20 : DevExpress.XtraReports.UI.XtraReport
    {
        private string ProductId;
        string detailTableName;
        ReportPrintTool printTool;
        private DataSet ds;
        int type = 0;

        public ImalatReport20()
        {
            InitializeComponent();
            printTool = new ReportPrintTool(this);
            this.ShowPrintMarginsWarning = false;
        }

        public void ArtikelRaporu(int ProductId)
        {
            //Type = Mevcut kayıt 
            type = 1;
            ds = Data.DM.ExecuteDataSet("GetKumasImalatFoyu", DBNull.Value, ProductId);
            Bind();
        }

        public void SiparisFormuRaporu(int POrderLinesId)
        {
            //Type = Yeni kayıt
            type = 2;
            ds = Data.DM.ExecuteDataSet("GetKumasImalatFoyu", POrderLinesId, DBNull.Value);
            Bind();
        }

        private void Bind()
        {
            this.DataSource = ds;
            this.DataMember = detailTableName;

            DataRow drHeader = ds.Tables[0].Rows[0];
            ProductId = drHeader["ProductId"].ToStringNN();
            detailTableName = ds.Tables[0].TableName;
            lblMusteri.Text = drHeader["Musteri"].ToStringNN();
            if (lblMusteri.Text == "")
                lblMusteri.Text = "Aker Tekstil Numune";

            lblSipNo.Text = drHeader["SiparisNo"].ToStringNN();
            if (drHeader["QtyManuel"].ToStringNN() == "")
                lblSipMiktari.Text = drHeader["SiparisMiktari"].ToDecimalNN().ToString("n2") + " " + drHeader["Birim"].ToStringNN();
            else
                lblSipMiktari.Text = drHeader["QtyManuel"].ToDecimalNN().ToString("n2");
            if (drHeader["Termin"].ToStringNN() == "")
                lblTermin.Text = "";
            else
                lblTermin.Text = ((DateTime)(drHeader["Termin"])).ToShortDateString();

            DateTime drModifiedOn = (DateTime)drHeader["ModifiedOn"];
            if (type == 1) //mevcut
            {
                lblDate.Text = drModifiedOn.ToShortDateString();
                lblTime.Text = drModifiedOn.ToLongTimeString();
            }
            else //yeni
            {
                lblDate.Text = DateTime.Now.ToShortDateString();
                lblTime.Text = DateTime.Now.ToLongTimeString();
            }

            lblGirisGr.Text = drHeader["GirisGr"].ToStringNN();
            lblSiklik.Text = drHeader["SIKLIK"].ToStringNN();
            lblOrguTuru.Text = drHeader["OrguTuru"].ToStringNN();
            lblIgneCesidi.Text = drHeader["IgneCesidi"].ToStringNN();
            lblZeminIpi.Text = drHeader["ZeminIpiTipi"].ToStringNN() + " - " + drHeader["ZeminIpi"].ToStringNN();
            lblHavalar.Text = drHeader["Havalar"].ToStringNN();
            lblMakasGrubu.Text = drHeader["MakasGrubu"].ToStringNN();
            lblHavBoyu.Text = drHeader["HavBoyu"].ToStringNN();
            lblRaporBoyu.Text = drHeader["RaporBoyu"].ToStringNN();

            lblTumbler.Text = drHeader["TumblerSuresiIsisi"].ToStringNN();
            lblSardon.Text = drHeader["SardonTipi"].ToStringNN();
            lblRam.Text = drHeader["Ram"].ToStringNN();
            lblDekatur.Text = drHeader["Dekatur"].ToStringNN();
            lblFinalGramaj.Text = drHeader["FinishGr"].ToStringNN();
            lblFinalEn.Text = drHeader["FinalEn"].ToStringNN();
            lblArtikelNo.Text = drHeader["ReferansArtikel"].ToStringNN(); // +drHeader["PartiNo"].ToStringNN();
            lblArtikel.Text = drHeader["Artikel"].ToStringNN();
            lblPartiNo.Text = drHeader["PartiNo"].ToStringNN();
            lblKompozisyon.Text = drHeader["Kompozisyon"].ToStringNN();
            lblCinsi.Text = drHeader["Cins"].ToStringNN();
            lblGrup.Text = drHeader["Grup"].ToStringNN();
            lblSanfor.Text = (bool)drHeader["Sanfor"] ? "Var" : "Yok";
            lblBantGr.Text = "8";
            lblDesen.Text = drHeader["DesenNo"].ToStringNN();
            lblAciklama.Text = drHeader["Aciklama"].ToStringNN();
            lblRenk.Text = drHeader["Renk"].ToStringNN();
            lblTarakGrubu.Text = drHeader["TarakGrubu"].ToStringNN();
            lblToplamHarmanKG.Text = GetKG(drHeader["HarmanKG"]);
            lblToplamHarmanKG.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight;

            lblKafa1.Text = "1.KAFA " + drHeader["Kafa1"].ToStringNN() + " " + drHeader["KafaRenk1"].ToStringNN();
            lblKafa2.Text = "2.KAFA " + drHeader["Kafa2"].ToStringNN() + " " + drHeader["KafaRenk2"].ToStringNN();
            lblKafa3.Text = "3.KAFA " + drHeader["Kafa3"].ToStringNN() + " " + drHeader["KafaRenk3"].ToStringNN();
            lblKafa4.Text = "4.KAFA " + drHeader["Kafa4"].ToStringNN() + " " + drHeader["KafaRenk4"].ToStringNN();


            lblKafa1kg.Text = GetKG(drHeader["Kafa1Kg"]);
            lblKafa2kg.Text = GetKG(drHeader["Kafa2Kg"]);
            lblKafa3kg.Text = GetKG(drHeader["Kafa3Kg"]);
            lblKafa4kg.Text = GetKG(drHeader["Kafa4Kg"]);




            lblFire.Text = drHeader["Fire"].ToStringNN();


            lblProductId.DataBindings.Add("Text", null, detailTableName + ".ProductId");

            DataRow[] drKafa1 = ds.Tables[1].Select("KafaNo=1");
            DataRow[] drKafa2 = ds.Tables[1].Select("KafaNo=2");
            DataRow[] drKafa3 = ds.Tables[1].Select("KafaNo=3");
            DataRow[] drKafa4 = ds.Tables[1].Select("KafaNo=4");

            if (drKafa1.Length + drKafa2.Length > 0)
                tablo1.Rows.Clear();
            if (drKafa3.Length + drKafa4.Length > 0)
                tablo2.Rows.Clear();

            HammaddeTablosuOlustur(tablo1, drKafa1, drKafa2);
            HammaddeTablosuOlustur(tablo2, drKafa3, drKafa4);
        }

        private string GetKG(object source)
        {
            decimal result = 0;
            decimal.TryParse(source.ToStringNN(), out result);


            if (result == 0)
                return "";
            else
                return source.ToStringNN() + " kg";
        }

        private void HammaddeTablosuOlustur(XRTable table, DataRow[] drKafa1, DataRow[] drKafa2)
        {
            int buyukSatirSayisi = drKafa1.Length;
            if (drKafa2.Length > buyukSatirSayisi)
                buyukSatirSayisi = drKafa2.Length;

            for (int i = 0; i < buyukSatirSayisi; i++)
            {
                XRTableRow xr = new XRTableRow();
                xr.Borders = DevExpress.XtraPrinting.BorderSide.Right;

                XRTableCell xcSag = HammaddeSatiriOlustur();
                XRTableCell xcSol = HammaddeSatiriOlustur();

                XRTableCell xcSag1 = HammaddeOranHucresi();
                XRTableCell xcSol1 = HammaddeOranHucresi();

                xr.Cells.Add(xcSol);
                xr.Cells.Add(xcSol1);

                xr.Cells.Add(xcSag);
                xr.Cells.Add(xcSag1);

                if (drKafa1.Length > i)
                {
                    xcSol.Text = " " + drKafa1[i]["GrupKodu"].ToStringNN() + " \n ";
                    xcSol1.Text = drKafa1[i]["HarmanKG"].ToStringNN() + "  kg";
                }


                if (drKafa2.Length > i)
                {
                    xcSag.Text = " " + drKafa2[i]["GrupKodu"].ToStringNN() + " \n ";
                    xcSag1.Text = drKafa2[i]["HarmanKG"].ToStringNN() + "  kg";
                }

                table.Rows.Add(xr);
            }
        }

        private static XRTableCell HammaddeSatiriOlustur()
        {
            XRTableCell xc = new XRTableCell();
            xc.Font = new Font("Verdana", 7);
            xc.WidthF = 289.01F;
            xc.Multiline = true;
            xc.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Left)));
            xc.BorderWidth = 1;
            xc.Padding = new DevExpress.XtraPrinting.PaddingInfo(5, 0, 5, 0);
            return xc;
        }

        private static XRTableCell HammaddeOranHucresi()
        {
            XRTableCell xc = new XRTableCell();
            xc.Font = new Font("Verdana", 7);
            xc.WidthF = 100;
            xc.Multiline = true;
            xc.Borders = DevExpress.XtraPrinting.BorderSide.Right;
            xc.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopRight;
            xc.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 5, 5, 0);
            xc.BorderWidth = 1;
            return xc;
        }
    }
}
